package com.mzxy.boxuegu.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.boxuegu.R;
import com.mzxy.boxuegu.utils.MD5Utils;
import com.mzxy.boxuegu.utils.SpUtils;

public class FindPswActivity extends AppCompatActivity {
    private TextView tvMainTitle;
    private TextView tvBack;

    private TextView tvUserName;
    private EditText etUserName;

    private boolean isFindPswPage = true;

    private TextView btnValidate;
    private EditText etValidateName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_psw);

        Intent data = getIntent();
        //如果 isfindpsw为true 证明是要显示找回密码界面，如果为false，则是证明要显示设置密保界面
        isFindPswPage = data.getBooleanExtra("isfindpsw",true);

        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        tvUserName = (TextView) findViewById(R.id.tv_user_name);
        etUserName = (EditText) findViewById(R.id.et_user_name);
        btnValidate = (TextView) findViewById(R.id.btn_validate);
        etValidateName = (EditText) findViewById(R.id.et_validate_name);


        if(isFindPswPage){
            tvMainTitle.setText("找回密码");
        }else{
            tvMainTitle.setText("设置密保");
            btnValidate.setText("保存");
            tvUserName.setVisibility(View.GONE);  //INVISIBLE 是会占据空间，仅仅使控件不可见，GONE 则会清除整个控件，即不可见也不占空间
            etUserName.setVisibility(View.GONE);
        }
        tvBack = (TextView) findViewById(R.id.tv_back);
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btnValidate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isFindPswPage){
                    //1. 获取用户输入的 用户名 和 验证姓名
                    String username = etUserName.getText().toString();
                    String validateName = etValidateName.getText().toString();
                    if(TextUtils.isEmpty(username) || TextUtils.isEmpty(validateName)){
                        Toast.makeText(FindPswActivity.this,"用户名和密保姓名均不能为空！",Toast.LENGTH_SHORT).show();
                        return;
                    }

                    //2. 判断密保是否正确，使用用户名去sp里面获取存储的密保进行对比
                    String oldValidateName = SpUtils.readValidateName(FindPswActivity.this,username);
                    if(!validateName.equals(oldValidateName)){
                        Toast.makeText(FindPswActivity.this,"密保姓名不正确！",Toast.LENGTH_SHORT).show();
                        return;
                    }

                    //3. 重置密码为123
                    SpUtils.saveRegisterInfo(FindPswActivity.this,username, "123");
                    Toast.makeText(FindPswActivity.this,"验证成功，密码重置为123",Toast.LENGTH_SHORT).show();
                    finish();
                }else{
                    //代表是设置密保界面
                    String validateName = etValidateName.getText().toString();
                    //保存到sp里面去，以当前登陆的username为key 以输入的validatename 为value存储
                    if(TextUtils.isEmpty(validateName)){
                        Toast.makeText(FindPswActivity.this,"密保姓名不能为空！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    SpUtils.saveValidateName(FindPswActivity.this,SpUtils.getLoginUsername(FindPswActivity.this),validateName);
                    //提示一下 然后退出
                    Toast.makeText(FindPswActivity.this,"密保设置成功！",Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });

    }
}
